<?php

class CreateNewProjectTask extends Task {

    public function __construct($argv) {
        parent::__construct($argv);
        $this->description = "Creates a new project.";
        $this->example = <<<EXAMPLE
ex. mini create_new_project blog
This will create a new project called blog under the current working directory.

ex. mini create_new_project blog --skip
This will try to create a new project called blog under the current working
directory. It will skip copying the file if the file already exists.

ex. mini create_new_project blog --pretend
This will pretend to create a new project called blog under the current working
directory. No files will be copied

EXAMPLE;
    }

    public function main() {
        $path = $this->argv['input'][1];

        if (empty($path)) {
            echo "Please specify a project name.\n";
            Task::help(array('taskInstance' => $this));
            return;
        }

        if (file_exists($path) && !is_dir($path)) {
            echo "Please specify a directory instead of a file to be the project name.\n";
            Task::help(array('taskInstance' => $this));
            return;
        }

        // copy the files
        Task::_copy(MINI_TEMPLATES.DS.'application', $path);

        // setup the env
        $data = file_get_contents(MINI_TEMPLATES.DS.'application'.DS.'config'.DS.'environment.php');
        $data = str_replace('%PATH_TO_MINI%', MINI, $data);
        file_put_contents($path.DS.'config'.DS.'environment.php', $data);

        // setup the log files
        chmod($path.DS."log".DS."development.log", 0666);
        chmod($path.DS."log".DS."test.log", 0666);
        chmod($path.DS."log".DS."staging.log", 0666);
        chmod($path.DS."log".DS."production.log", 0666);
    }
}